package edu.unlv.cs.ladders.contraints;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class PhoneValidator implements ConstraintValidator<Phone, String> {
	private static final Pattern PHONE_NUMBER = Pattern.compile("\\d{7,11}+");
	public void initialize(Phone constraintAnnotation) {
		//nop
	}

	public boolean isValid(String value, ConstraintValidatorContext context) {
		boolean isValid=false;
		System.out.println(value);
		String inputStr = value.replaceAll("\\s", "");
		inputStr = inputStr.replaceAll("-", "");
		inputStr = inputStr.replaceAll("\\.", "");
		inputStr = inputStr.replaceAll("\\(", "");
		inputStr = inputStr.replaceAll("\\)", "");
		//LOG.debug("Validating phone:"+inputStr);
		System.out.println("Validating phone:"+inputStr);
		Matcher matcher = PHONE_NUMBER.matcher(inputStr);  
		if(matcher.matches()){  
			isValid = true;  
		}  
		return isValid;  
	}

}
